/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.configure.Config;
import com.lineage.server.PacketCreate;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.Random;

/**
 * Class <code>EnchantConversion</code> 附魔转换卷轴. <br>
 * 近战附魔转换卷轴 - 47049 <br>
 * 远攻附魔转换卷轴 - 47050 <br>
 * 恢复附魔转换卷轴 - 47051 <br>
 * 防御附魔转换卷轴 - 47052 <br>
 * 
 * @author jrwz
 * @version 2012-6-19下午01:37:59
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class EnchantConversion extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new EnchantConversion();
    }

    /** 附魔转换卷轴. */
    private EnchantConversion() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        final int itemId = useItem.getItemId();
        final int itemobj = targetId;
        final L1ItemInstance l1iteminstance1 = pc.getInventory().getItem(
                itemobj);
        if ((l1iteminstance1.getItemId() >= 47053)
                && (l1iteminstance1.getItemId() <= 47062)) { // 附魔石 ~ 9阶附魔石
            final int type = (itemId - 47048) * 10; // type = 10,20,30,40
            final int rnd = Random.nextInt(100) + 1;
            if (Config.MAGIC_STONE_TYPE < rnd) {
                final int newItem = l1iteminstance1.getItemId() + type; // 附魔石(近战)
                                                                        // ~
                                                                        // 9阶附魔石(近战)
                final L1Item template = ItemTable.getInstance().getTemplate(
                        newItem);
                if (template == null) {
                    PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
                }
                pc.createNewItem(pc, newItem, 1); // 获得附魔石(XX)
            } else {
                pc.sendPackets(new S_ServerMessage(1411, l1iteminstance1
                        .getName())); // 对\f1%0附加魔法失败。
            }
            pc.getInventory().removeItem(l1iteminstance1, 1); // 删除 - 附魔石
            pc.getInventory().removeItem(useItem, 1); // 删除 - 附魔转换卷轴
        } else {
            PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
        }
    }
}
